﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DBService;
using DB2Service;

namespace RXCourt
{
   public class CommonFY:ICourt
    {
        private  IDBService dbService ;
        private string ConnectionString;
        private string currentFydm="";
        public CommonFY(IDBService dbService, string ConnectionString)
       {
           this.ConnectionString = ConnectionString;
           this.dbService = dbService;

       }
       static int getTaget(string c)
       {
           int a = 0;
           if (c[0] != '0' && c[1] == '0' && c[2] == '0')
           {
               a = 1;
           }

           if (c[0] != '0' && c[1] != '0' && c[2] == '0')
           {
               a = 2;
           }

           if (!c.Contains("0"))
           {
               a = 3;
           }
           return a;
       }
       

     #region ICourt 成员
    

     DataTable ICourt.GetChildrenFY(string fydm)
     {
         throw new NotImplementedException();
     }
    
     DataTable ICourt.GetSelfAndChildrenFY(string fydm)
     {
         string c = "K20";
         string likeStr = "";
         if (c == "000")
         {
             likeStr = "%00";
         }

         switch (getTaget(c))
         {
             case 1:
                 likeStr = string.Format("{0}%0", c[0]);
                 break;
             case 2:
                 likeStr = string.Format("{0}{1}%", c[0], c[1]);
                 break;
             case 3:
                 likeStr = c;
                 break;
                 
         }
         string sql = SQLStringService.GetSelectStr_Fy(likeStr, fydm);
         return dbService.Select(sql, ConnectionString);
     }

     DataTable ICourt.GetFY(string fydm)
     {
         throw new NotImplementedException();
     }

     string ICourt.GetFY_IDColumnName()
     {
         return "dm";
     }

     string ICourt.GetFY_MCColumnName()
     {
         return "mc";
     }

   

     DataTable ICourt.GetTSs(string fydm)
     {
         string sql = SQLStringService.GetSelectStr_TSJG(fydm);
         return dbService.Select(sql, ConnectionString);
     }

     DataTable ICourt.GetRYs(string fydm, string tsdm)
     {
         string sql = "select * from B_RYXX where szjg=" + tsdm;
         return dbService.Select(sql, ConnectionString);
     }

     


     string ICourt.GetTS_MCColumnName()
     {
         return "mc";
     }

     string ICourt.GetTS_IDColumnName()
     {
         return "jgbs";
     }

    


     string ICourt.GetRY_IDColumnName()
     {
         return "rybs";
     }

     string ICourt.GetRY_MCColumnName()
     {
         return "xm";
     }

  

     string ICourt.GetCurrentFYDM()
     {
         return currentFydm;
     }

   
     void ICourt.SetCurrentFYDM(string fydm)
     {
         currentFydm = fydm;
     }

    

     string ICourt.GetFBDM(string fydm)
     {
         throw new NotImplementedException();
     }




     string ICourt.GetFYMC(string fydm)
     {
         ICourt iCourt = this;
         string sql = string.Format("select {0} from {1} where {2}={3}", iCourt.GetFY_MCColumnName(),"b_fy",iCourt.GetFY_IDColumnName(),fydm);
         object o = dbService.ExcecuteScalar(sql, ConnectionString);
         if (o == null) return "";
         return o.ToString(); 
     }

     string ICourt.GetTSMC(string fydm, string tsdm)
     {
         ICourt iCourt = this;
         string sql = string.Format("select {0} from {1} where FY={4}  and {2}={3}", iCourt.GetTS_MCColumnName(), "B_TSJG", iCourt.GetTS_IDColumnName(), tsdm,fydm);
         object o = dbService.ExcecuteScalar(sql, ConnectionString);
         if (o == null) return "";
         return o.ToString(); 
     }

     #endregion
    }
}
